package com.hd.controller.sqlexcel;
import java.util.List;

import com.hd.entity.sqlexcel.SqlExcelReport;
import com.hd.generator.StringUtil;
import com.hd.service.sqlexcel.SqlExcelReportService;
import com.hd.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hd.controller.base.BaseController;
import com.hd.entity.sqlexcel.SqlExcelQueryExpression;
import com.hd.service.sqlexcel.SqlExcelQueryExpressionService;
import com.hd.util.JsonUtil;
import com.hd.util.result.PageInfo;

/**
* sql导出excel字段查询表达式
*/
@RequestMapping("sqlExcelQueryExpression")
@Controller
public class SqlExcelQueryExpressionController extends BaseController {
    @Autowired
    private SqlExcelQueryExpressionService sqlExcelQueryExpressionService;
    @Autowired
    private SqlExcelReportService sqlExcelReportService;
    /**
     * 主页
     * @return
     */
    @RequestMapping("manager")
    public String manager(String id,Model model) {
    	model.addAttribute("id", id);
        SqlExcelReport sqlExcelReport=sqlExcelReportService.get(id);
    	model.addAttribute("sqlExcelReport",sqlExcelReport);
        return "jsp/report/sqlExcelQueryExpression/sqlExcelQueryExpressionList";
    }
    /**
     * 列表
     * @return
     */
    @RequestMapping(value="/dataGrid",method=RequestMethod.POST)
    @ResponseBody
    public Object findAllById(String fid) {
    	List<SqlExcelQueryExpression> vos = sqlExcelQueryExpressionService.findAllByFId(fid);
        return vos;
    }

    /**
     * 添加页
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage(String fId,Model model) {
    	model.addAttribute("fId", fId);
        return "jsp/report/sqlExcelQueryExpression/sqlExcelQueryExpressionAdd";
    }

    /**
     * 添加
     * @param sqlExcelQueryExpression
     * @return
     */
    @RequestMapping(value="/add",method=RequestMethod.POST)
    @ResponseBody
    public Object add(SqlExcelQueryExpression sqlExcelQueryExpression) {
        if(StringUtils.isNotNull(sqlExcelQueryExpression.getExpression())){
            String expression=sqlExcelQueryExpression.getExpression();
            expression=expression.replaceAll("&#39;","'");
            sqlExcelQueryExpression.setExpression(expression);
        }
        sqlExcelQueryExpressionService.save(sqlExcelQueryExpression);
        return renderSuccess("添加成功！");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Object delete(@PathVariable("id") String id) {
        sqlExcelQueryExpressionService.delete(id);
        return renderSuccess("删除成功！");
    }
    /**
     * 编辑页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/editPage/{id}")
	public String editPage(Model model, @PathVariable("id")String id) {
	 SqlExcelQueryExpression sqlExcelQueryExpression = sqlExcelQueryExpressionService.get(id);
        if(StringUtils.isNotNull(sqlExcelQueryExpression.getExpression())){
            String expression=sqlExcelQueryExpression.getExpression();
            expression=expression.replaceAll("'","&#39;");
            sqlExcelQueryExpression.setExpression(expression);
        }
        model.addAttribute("sqlExcelQueryExpression", sqlExcelQueryExpression);
        return "jsp/report/sqlExcelQueryExpression/sqlExcelQueryExpressionEdit";
    }

    /**
     * 查看页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/viewPage/{id}")
    public String viewPage(Model model, @PathVariable("id") String id) {
        SqlExcelQueryExpression sqlExcelQueryExpression = sqlExcelQueryExpressionService.get(id);
        model.addAttribute("sqlExcelQueryExpression", sqlExcelQueryExpression);
        return "jsp/report/sqlExcelQueryExpression/sqlExcelQueryExpressionView";
    }
    /**
     * 编辑
     * @param sqlExcelQueryExpression
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ResponseBody
    public Object edit(SqlExcelQueryExpression sqlExcelQueryExpression) {
        if(StringUtils.isNotNull(sqlExcelQueryExpression.getExpression())){
            String expression=sqlExcelQueryExpression.getExpression();
            expression=expression.replaceAll("&#39;","'");
            sqlExcelQueryExpression.setExpression(expression);
        }
        sqlExcelQueryExpressionService.update(sqlExcelQueryExpression);
        return renderSuccess("编辑成功！");
    }


}
